package com.qfdu.repo.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.qfdu.repo.bean.Product;
import com.qfdu.repo.bean.RespResult;
import com.qfdu.repo.common.Const;
import com.qfdu.repo.common.JSONUtils;
import com.qfdu.repo.dao.ProductDao;
import com.qfdu.repo.dao.impl.ProductDaoImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.util.List;

/**
 * @author 沐辰
 * @version v1.0
 * @description:
 * @project Qfed2401
 * @Date 2024/5/9 11:05
 * @company qf
 */
@WebServlet(name = "ProductController",urlPatterns = "/product")
public class ProductController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("application/json;charset=utf-8");
//        resp.setCharacterEncoding("UTF-8");
        String method = req.getParameter("method");
        if (method != null) {
            if (method.equals("allList")) {
                allList(req,resp);
            }
            if (method.equals("add")) {
                add(req,resp);
            }
        }
    }

    /**
     * 页面展示
     * @param req
     * @param resp
     */
    private void allList(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        PrintWriter printWriter = null;
        ProductDao productDao = new ProductDaoImpl();
        List<Product> products = productDao.selectAll();
        RespResult respResult = new RespResult();
        if (products != null) {
//            JSONUtils.toJSONString(Const.PRODUCT_SUCCESS,"获取成功",products);
            respResult.setMsg("获取列表成功");
            respResult.setCode(Const.PRODUCT_SUCCESS);
            respResult.setRepo(products);

        } else {
//            JSONUtils.toJSONString(Const.PRODUCT_EMPTY,"获取失败");
            respResult.setMsg("未获取到列表数据");
            respResult.setCode(Const.PRODUCT_EMPTY);
        }
        printWriter = resp.getWriter();
        printWriter.write(JSONUtils.toJSONString(respResult));
        printWriter.flush();
        printWriter.close();
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("text/html;charset=utf-8");
        String pno = req.getParameter("pno");
        String pname = req.getParameter("pname");
        String paddr = req.getParameter("paddr");
        String pdate = req.getParameter("pdate");
        String enterdate = req.getParameter("enterdate");
        Product product = new Product();
        product.setPno(pno);
        product.setPname(pname);
        product.setPaddr(paddr);
        product.setPdate(pdate);
        product.setEnterdate(enterdate);
        ProductDao productDao = new ProductDaoImpl();
        productDao.add(product);
        try {
            req.getRequestDispatcher("/index.html").forward(req,resp);
        } catch (ServletException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
